package com.example.demo;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.*;
import java.util.List;

public class WordToPdfConverter {

    public static void main(String[] args) {
        try {
            XWPFDocument doc = new XWPFDocument(new FileInputStream("E:\\windows\\下载\\货物系固手册编制指南.docx"));
            OutputStream out = new FileOutputStream("output.pdf");
            Document pdfDoc = new Document();
            PdfWriter.getInstance(pdfDoc, out);
            pdfDoc.open();

            List<XWPFParagraph> paragraphs = doc.getParagraphs();
            for (XWPFParagraph para : paragraphs) {
                List<XWPFRun> runs = para.getRuns();
                for (XWPFRun run : runs) {
                    String text = run.getText(0);
                    if (text != null) {
                        pdfDoc.add(new Paragraph(text));
                    }
                }
            }

            pdfDoc.close();
            out.close();

            System.out.println("Word文档成功转换为PDF！");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}